package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.UserBlackListDao;
import com.tbit.main.dao.core.UserDao;
import com.tbit.main.pojo.User;
import com.tbit.main.pojo.UserBlackList;
import com.tbit.main.service.UserBlackListService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("userBlackListService")
public class UserBlackListServiceImpl implements UserBlackListService {
    @Autowired
    private UserBlackListDao userBlackListDao;
    @Autowired
    private UserDao userDao;

    @Override
    public void insert(UserBlackList userBlackList) {
        userBlackListDao.insert(userBlackList);
    }

    @Override
    public void insertBath(List<UserBlackList> userBlackLists) {
        userBlackListDao.insertBath(userBlackLists);
    }

    @Override
    public void update(UserBlackList userBlackList) {
        userBlackListDao.update(userBlackList);
    }

    @Override
    public void del(Integer userId) {
        userBlackListDao.del(userId);
    }

    @Override
    public void delIdNO(String idNO) {
        userBlackListDao.delIdNO(idNO);
    }

    @Override
    public UserBlackList getByUserId(Integer userId) {
        return userBlackListDao.getByUserId(userId);
    }

    @Override
    public List<UserBlackList> getByAccountId(Integer accountId) {
        return userBlackListDao.getByAccountId(accountId);
    }

    @Override
    public Integer getByAccountIdCount(Integer accountId) {
        return userBlackListDao.getByAccountIdCount(accountId);
    }

    @Override
    public List<UserBlackList> getByAccountIdPaing(Integer accountId, Integer pegeNo, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("start", (pegeNo - 1) * rowCount);
        map.put("rowCount", rowCount);
        return userBlackListDao.getByAccountIdPaing(map);
    }

    @Override
    public List<UserBlackList> getByIdNO(Integer accountId, String idNO) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("idNO", idNO);
        return userBlackListDao.getByIdNO(map);
    }

    @Override
    public void setUserBlackListByPhoneBatch(Integer accountId,
                                             String phones, String expireTime,String photosUrl) {
        List<String> list = Arrays.asList(phones.split(","));
        List<String> needCreatePhones = list;
        /**查询已存在用户*/
        List<User> existUsers = userDao.getByPhones(list);
        /**已存在用户Id*/
        List<Integer> existIds = existUsers.stream().map(User::getUserId).collect(Collectors.toList());
        /**已存在用户手机号码*/
        List<String> existPhones = existUsers.stream().map(User::getPhone).collect(Collectors.toList());

        needCreatePhones.removeAll(existPhones);

        String operateTime = DateTimeUtil.getNowTime();
        /**需要新增用户*/
        if (!needCreatePhones.isEmpty()) {
            List<User> users = createUser(needCreatePhones, operateTime, accountId);
            /***/
            addBatch(users, expireTime,photosUrl);
        }
        /**已存在用户需要查询是否存在黑名单*/
        if (!existUsers.isEmpty()) {
            /**已存在黑名单用户*/
            List<Integer> ids = userBlackListDao.getByUserIds(existIds);

            List<User> addUsers = new ArrayList<>();
            /**存在就更新*/
            for (User user : existUsers) {
                if (ids.contains(user.getUserId())) {
                    UserBlackList userBlackList = new UserBlackList();
                    userBlackList.setUserId(user.getUserId());
                    userBlackList.setExpireTime(expireTime);
                    userBlackList.setRemark("预拉黑更新");
                    userBlackList.setPhotosUrl(photosUrl);
                    userBlackListDao.update(userBlackList);
                } else {
                    addUsers.add(user);
                }
            }
            if (!addUsers.isEmpty()) {
                /**批量新增*/
                addBatch(addUsers, expireTime,photosUrl);
            }
        }

    }

    /**
     * 批量插入
     *
     * @param users
     * @param expireTime
     */
    public void addBatch(List<User> users, String expireTime,String photosUrl) {
        int num = users.size() / 300;
        int model = users.size() % 300;
        /***/
        for (int i = 0; i < num; i++) {
            List<User> addUsers = users.subList(i * 300, (i + 1) * 300);
            userDao.addBatch(addUsers);
            /**添加黑名单*/
            addUserBlack(addUsers, expireTime,photosUrl);
        }
        if (model > 0) {
            List<User> addUsers = users.subList(num * 300, users.size());
            userDao.addBatch(addUsers);
            /**添加黑名单*/
            addUserBlack(addUsers, expireTime,photosUrl);
        }
    }

    /**
     * 封装拉黑信息
     *
     * @param addUsers   需要拉黑用户
     * @param expireTime 到期时间
     */
    public void addUserBlack(List<User> addUsers, String expireTime,String photosUrl) {
        List<UserBlackList> userBlackLists = new ArrayList<>();
        for (User u : addUsers) {
            UserBlackList userBlackList = new UserBlackList();
            userBlackList.setAccountId(u.getAccountId());
            userBlackList.setAddTime(u.getJoinTime());
            userBlackList.setIdNO(null);
            userBlackList.setUserId(u.getUserId());
            userBlackList.setExpireTime(expireTime);
            userBlackList.setPhone(u.getPhone());
            userBlackList.setRemark("预拉黑用户");
            userBlackList.setPhotosUrl(photosUrl);
            userBlackLists.add(userBlackList);
        }
        insertBath(userBlackLists);
    }

    /**
     * 创建用户
     *
     * @param phones    手机号码
     * @param date      添加时间
     * @param accountId 品牌id
     * @return
     */
    public List<User> createUser(List<String> phones, String date, Integer accountId) {
        List<User> users = new ArrayList<>();
        for (String phone : phones) {
            User user = new User();
            user.setPhone(phone);
            user.setAccountId(accountId);
            user.setCountry(86);
            user.setJoinTime(date);
            user.setUpdateTime(date);
            user.setRemark("预拉黑添加用户");
            users.add(user);
        }
        return users;
    }

}  